package org.xwalk.core;

import java.io.BufferedWriter;
import java.io.File;
import java.util.Map;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.ArrayList;



public class XWalkHitTestResult   {

    private ArrayList<Object> constructorTypes;
    private ArrayList<Object> constructorParams;
    private ReflectMethod postWrapperMethod;


    public enum type {
        UNKNOWN_TYPE,
        ANCHOR_TYPE,
        PHONE_TYPE,
        GEO_TYPE,
        EMAIL_TYPE,
        IMAGE_TYPE,
        IMAGE_ANCHOR_TYPE,
        SRC_ANCHOR_TYPE,
        SRC_IMAGE_ANCHOR_TYPE,
        EDIT_TEXT_TYPE
    }

    private ReflectMethod enumtypeClassValueOfMethod = new ReflectMethod();

    private Object Converttype(type type) {
        return enumtypeClassValueOfMethod.invoke(type.toString());
    }

    private XWalkCoreWrapper coreWrapper;
    private Object bridge;

    protected Object getBridge() {
        return bridge;
    }

    public XWalkHitTestResult(Object bridge) {
        this.bridge = bridge;
        reflectionInit();
    }

    /**
     * Gets the type of the hit test result. See the XXX_TYPE constants
     * defined in this class.
     *
     * @return the type of the hit test result
     * @since 7.0
     */
    public type getType() {
        try {
            return (type) type.valueOf(getTypeMethod.invoke().toString());
        } catch (UnsupportedOperationException e) {
            if (coreWrapper == null) {
                throw new RuntimeException("Crosswalk's APIs are not ready yet");
            } else {
                XWalkCoreWrapper.handleRuntimeError(e);
            }
        }
        return null;
    }

    private ReflectMethod getTypeMethod = new ReflectMethod(null, "getType");

    /**
     * Gets additional type-dependant information about the result. See
     * {@link WebView#getHitTestResult()} for details. May either be null
     * or contain extra information about this result.
     *
     * @return additional type-dependant information about the result
     * @since 7.0
     */
    public String getExtra() {
        try {
            return (String)getExtraMethod.invoke();
        } catch (UnsupportedOperationException e) {
            if (coreWrapper == null) {
                throw new RuntimeException("Crosswalk's APIs are not ready yet");
            } else {
                XWalkCoreWrapper.handleRuntimeError(e);
            }
        }
        return null;
    }

    private ReflectMethod getExtraMethod = new ReflectMethod(null, "getExtra");


    void reflectionInit() {
        XWalkCoreWrapper.initEmbeddedMode();

        coreWrapper = XWalkCoreWrapper.getInstance();
        if (coreWrapper == null) {
            XWalkCoreWrapper.reserveReflectObject(this);
            return;
        }

        enumtypeClassValueOfMethod.init(null,
                coreWrapper.getBridgeClass("XWalkHitTestResultInternal$type"), "valueOf", String.class);

        getTypeMethod.init(bridge, null,
                "getTypeSuper");
        getExtraMethod.init(bridge, null,
                "getExtraSuper");
    }

}
